//
//  ModifyPhoneNumberViewController.m
//  League
//
//  Created by 崔璨 on 2018/1/30.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#import "ModifyPhoneNumberViewController.h"
#import "UserInfomation.h"

@interface ModifyPhoneNumberViewController ()
{
    UIButton *getCodeBtn;
}
@property (nonatomic ,assign)NSInteger CountDown;
//定时器

@property (nonatomic ,strong) NSTimer *countDownTimer;
@property (nonatomic ,strong) NSTimer *Timer;
@property (strong, nonatomic) NSString *registerCode;

@end

@implementation ModifyPhoneNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [InternationalControl initUserLanguage];//初始化应用语言
    
    NSBundle *bundle = [InternationalControl bundle];

        self.title =  [bundle localizedStringForKey:@"ChangeMyPhone" value:nil table:@"hello"];
    
    [self setupUI];
    // Do any additional setup after loading the view.
}
-(void)setupUI
{
    
    [InternationalControl initUserLanguage];//初始化应用语言
    
    NSBundle *bundle = [InternationalControl bundle];
    UITextField *oldNumberTf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.width/1.2, self.view.height/13)];
    oldNumberTf.center = CGPointMake(self.view.width/2, self.view.height/2 - oldNumberTf.height*2);
    oldNumberTf.backgroundColor = [UIColor whiteColor];
    oldNumberTf.placeholder=[bundle localizedStringForKey:@"oldPhoneNum" value:nil table:@"hello"];
    oldNumberTf.textColor = kAppColor;
    oldNumberTf.layer.masksToBounds = YES;
    oldNumberTf.layer.cornerRadius = 8;
    oldNumberTf.layer.borderWidth = 2;
    oldNumberTf.hidden = YES;
    oldNumberTf.layer.borderColor = [kAppColor CGColor];
    [self.view addSubview:oldNumberTf];
    
    UITextField *newNumberTf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, oldNumberTf.width, oldNumberTf.height)];
    newNumberTf.center = CGPointMake(self.view.width/2, oldNumberTf.y- newNumberTf.height);
    newNumberTf.tag = 300;
    newNumberTf.backgroundColor = [UIColor whiteColor];
    newNumberTf.placeholder = [bundle localizedStringForKey:@"newPhoneNum" value:nil table:@"hello"];
    newNumberTf.textColor = kAppColor;
    newNumberTf.layer.masksToBounds = YES;
    newNumberTf.layer.cornerRadius = 8;
    newNumberTf.layer.borderWidth = 2;
    newNumberTf.layer.borderColor = [kAppColor CGColor];
    [self.view addSubview:newNumberTf];
    
    UITextField *codeTf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.view.width/1.8, self.view.height/13)];
//    codeTf.center = CGPointMake(newNumberTf.x+codeTf.width/2, oldNumberTf.frame.origin.y+oldNumberTf.height+codeTf.height);
    codeTf.center = CGPointMake(newNumberTf.x+codeTf.width/2, self.view.height/2 - codeTf.height*2);
    
//    codeTf.placeholder = @"请输入验证码";
    codeTf.tag = 1001;
    codeTf.backgroundColor = [UIColor whiteColor];
    codeTf.textColor = kAppColor;
    codeTf.layer.masksToBounds = YES;
    codeTf.layer.cornerRadius = 8;
    codeTf.layer.borderWidth = 2;
    codeTf.layer.borderColor = [kAppColor CGColor];
    codeTf.font=[UIFont fontWithName:kFontName size:codeTf.height/3.5];
    codeTf.placeholder = [bundle localizedStringForKey:@"getCode" value:nil table:@"hello"];
    [self.view addSubview:codeTf];
    
    getCodeBtn =[[UIButton alloc] initWithFrame:CGRectMake(codeTf.x+codeTf.width+5, codeTf.y, newNumberTf.width - codeTf.width-5, codeTf.height)];
    getCodeBtn.backgroundColor = [UIColor clearColor];
    getCodeBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [getCodeBtn setTitle:[bundle localizedStringForKey:@"getCode" value:nil table:@"hello"] forState:UIControlStateNormal];
    [getCodeBtn setTitleColor:kAppColor forState:UIControlStateNormal];
    getCodeBtn.layer.masksToBounds = YES;
    getCodeBtn.layer.cornerRadius = 8;
    getCodeBtn.layer.borderWidth = 2;
    getCodeBtn.layer.borderColor = [kAppColor CGColor];
    getCodeBtn.titleLabel.font=[UIFont fontWithName:kFontName size:getCodeBtn.height/3.5];
    [getCodeBtn addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:getCodeBtn];
    
    UIButton *overBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, oldNumberTf.width, oldNumberTf.height)];
    overBtn.center = CGPointMake(self.view.width/2, getCodeBtn.y+getCodeBtn.height+overBtn.height);
    overBtn.backgroundColor = kAppColor;
    overBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [overBtn setTitle:[bundle localizedStringForKey:@"over" value:nil table:@"hello"] forState:UIControlStateNormal];
    [overBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    overBtn.layer.masksToBounds = YES;
    overBtn.layer.cornerRadius = 8;
    overBtn.titleLabel.font=[UIFont fontWithName:kFontName size:overBtn.height/3];
    [overBtn addTarget:self action:@selector(overBtn:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:overBtn];
    
}
- (void)backStartTime

{
    
    //设置倒计时总时长
    getCodeBtn.enabled = NO;
    [self.Timer invalidate];
    self.CountDown = 60;
    self.Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(TimeNSTimer) userInfo:nil repeats:YES];
    
}

//使用NSTimer实现倒计时
- (void)TimeNSTimer

{
    self.CountDown -- ;
    [getCodeBtn setTitle:[NSString stringWithFormat:@"%@",@(self.CountDown)] forState:UIControlStateNormal];
    if (self.CountDown == 0) {
        getCodeBtn.enabled = YES;
        [self.Timer setFireDate:[NSDate distantFuture]];
        [InternationalControl initUserLanguage];//初始化应用语言
        NSBundle *bundle = [InternationalControl bundle];
        [getCodeBtn setTitle:[bundle localizedStringForKey:@"getCode" value:nil table:@"hello"] forState:UIControlStateNormal];
        
    }
    
}
- (void)getCode:(UIButton *)btn{
    UITextField *newtext = [self.view viewWithTag:300];
    NSDictionary *dic = @{
                       @"phone":newtext.text
                       };
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kFirstUrl,kRegisterCode];
    NSLog(@"urlStr:%@",urlStr);
    [DataContrller PostRequestUrl:urlStr Param:dic requestSuccess:^(NSDictionary *backDic) {
        NSLog(@"backDic:%@",backDic);
        if ([backDic[@"returnCode"] integerValue] == 200) {
            //            self.registerView.userCodeTf.text = backDic[@"obj"][@"phonecode"];
            self.registerCode = backDic[@"obj"][@"phonecode"];
        } else if ([backDic[@"returnCode"] integerValue] == 404)  {
            [self showAlert:@"手机号码不存在"];
        } else {
            [self showAlert:backDic[@"msg"]];
        }
    } requestFail:^(NSString *backErrorStr) {
        
        NSLog(@"backErrorStr:%@",backErrorStr);
        [self showAlert:backErrorStr];
        
    }];
    [self backStartTime];
}
-(void)overBtn:(UIButton *)btn
{
    UITextField *newtext = [self.view viewWithTag:300];
    UITextField *codeTextField = [self.view viewWithTag:1001];
    if ([newtext.text isEqualToString:@""]) {
        [self showAlert:@"请输入手机号"];
        return;
    }
    if (![self.registerCode isEqualToString:codeTextField.text]) {
        [self showAlert:@"验证码错误"];
        return;
    }
    NSDictionary *dic = @{
                          @"phone": newtext.text,
                          @"pk":[UserInfomation shareManager].pk
                          };
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", kFirstUrl, kResetPhone];
    NSLog(@"urlStr:%@",urlStr);
    [DataContrller PostRequestUrl:urlStr Param:dic requestSuccess:^(NSDictionary *backDic) {
        NSLog(@"backDic:%@",backDic);
        if ([backDic[@"returnCode"] integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self showAlert:backDic[@"msg"]];
        }
    } requestFail:^(NSString *backErrorStr) {
        
        NSLog(@"backErrorStr:%@",backErrorStr);
        [self showAlert:backErrorStr];
    }];
    
}


@end
